﻿namespace Webinator.Util
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Web.Script.Serialization;

    /// <summary>
    /// Handles Deserializing Json by using Dynamic objects.
    /// </summary>
    public class DynamicJsonConverter : JavaScriptConverter
    {
        /// <summary>
        /// Gets supported types.
        /// </summary>
        public override IEnumerable<Type> SupportedTypes
        {
            get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); }
        }

        /// <summary>
        /// Deserializes json.
        /// </summary>
        /// <param name="dictionary">The dictionary.</param>
        /// <param name="type">The type.</param>
        /// <param name="serializer">The serializer.</param>
        /// <returns>
        /// The deserialized json.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// Thrown if the dictionary is null.
        /// </exception>
        public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("dictionary");
            }

            return type == typeof(object) ? new DynamicJsonObject(dictionary) : null;
        }

        /// <summary>
        /// Not Implemented
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="serializer">The serializer.</param>
        /// <returns><c>Not Implemented</c></returns>
        /// <exception cref="NotImplementedException">Not Implemented</exception>
        public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
        {
            throw new NotImplementedException();
        }
    }
}
